<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="tsx" setup>
  import { ref, computed, unref, reactive, toRefs } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { addArea, getArea, updateArea } from '/@/api/system/area';
  import { usePermission } from '/@/hooks/web/usePermission';
  const { t } = useI18n();
  const accountFormSchema: FormSchema[] = [
    {
      field: 'code',
      label: t('编号'),
      component: 'Input',
      required: true,
      colProps: { span: 24 },
    },
    {
      field: 'name',
      label: t('名称'),
      component: 'Input',
      required: true,
      colProps: { span: 24 },
    },
    {
      field: 'simpleSpelling',
      label: t('简拼'),
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'quickQuery',
      label: t('快速查询'),
      component: 'Input',
      colProps: { span: 24 },
    },
    {
      field: 'sortCode',
      label: t('排序'),
      component: 'InputNumber',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        min: 1,
      },
    },
    {
      field: 'remark',
      label: t('备注'),
      component: 'InputTextArea',
      colProps: { span: 24 },
    },
  ];

  const emit = defineEmits(['success', 'register']);

  const { filterFormSchemaAuth } = usePermission();
  const { notification } = useMessage();
  const isUpdate = ref(true);
  const rowId = ref('');

  console.log(filterFormSchemaAuth(accountFormSchema, true), 'ssssssssss');

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: filterFormSchemaAuth(accountFormSchema, true),
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });

  let areaInfo = reactive({
    layer: 1,
    parentId: null,
  });
  let { layer, parentId } = toRefs(areaInfo);

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });

    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      rowId.value = data.id;
      const record = await getArea(data.id);

      setFieldsValue({
        ...record,
      });
    } else {
      layer.value = data?.layer;
      parentId.value = data?.parentId;
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? t('新增区域') : t('编辑区域')));

  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      // TODO custom api
      if (!unref(isUpdate)) {
        //false 新增
        let paramas = parentId.value
          ? { ...areaInfo, ...values }
          : { layer: layer.value, ...values };
        await addArea(paramas);
        notification.success({
          message: t('提示'),
          description: t('新增成功'),
        }); //提示消息
      } else {
        values.id = rowId.value;
        await updateArea(values);
        notification.success({
          message: t('提示'),
          description: t('修改成功！'),
        }); //提示消息
      }

      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
